﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Wind.Utils;

namespace Wind.Cloud.Tools
{
    public class CloudUtils
    {
        #region File Operation Mothed
        /// <summary>
        /// 读文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string ReadFile(string fileName)
        {
            string result = string.Empty;
            try
            {
                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (StreamReader sr = new StreamReader(fs, Encoding.Default))
                    {
                        result = sr.ReadToEnd();
                        sr.Close();
                    }
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write("Wind.Cloud.Business.FileBusiness.ReadFile error", ex);
            }
            return result;
        }
        public static string ReadFile(string fileName, out Encoding encoding)
        {
            string result = string.Empty;
            encoding = null;
            try
            {
                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        result = sr.ReadToEnd();
                        encoding = sr.CurrentEncoding;
                        sr.Close();
                    }
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write("Wind.Cloud.Business.FileBusiness.ReadFile error", ex);
            }
            return result;
        }
        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="content"></param>
        public static void WriteFile(string fileName, string content, Encoding encoding)
        {
            try
            {
                string path = Path.GetDirectoryName(fileName);
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);
                using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
                {
                    using (StreamWriter sw = new StreamWriter(fs, encoding))
                    {
                        sw.Write(content);
                        sw.Close();
                    }
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write("Wind.Cloud.Business.FileBusiness.WriteFile error", ex);
            }
        }
        #endregion

    }
}
